/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.fasepa.sgpf.util;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 *
 * @author marcosdj
 */
public class JsfUtil {

    public static FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    public static ExternalContext getExternalContext() {
        return getFacesContext().getExternalContext();
    }

    public static HttpServletRequest getRequest() {
        return ((HttpServletRequest) getExternalContext().getRequest());
    }

    public static HttpServletResponse getResponse() {
        return ((HttpServletResponse) getExternalContext().getResponse());
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * Verifica os atributos do usuário autenticado.
     *
     * @return objeto do tipo authentication do Spring security, que possui o
     * nome e login do usuário autenticado.
     */
    public static Authentication getLoggedUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (context instanceof SecurityContext) {
            Authentication authentication = context.getAuthentication();
            return authentication;
        }
        return null;
    }

}
